!function () {

    var submitOrder = window.WEBBASE.extend({
        init: function () {//初始化
            // 收货地址选择地区
            this.selectArea();
        },
        events: {
            "#J_num-add click": "goodsNumChange",//商品数量加
            "#J_num-subtract click": "goodsNumChange",//商品数量减
            "#J_sel-invoice click": "selInvoiceClick",//选择发票类型弹框
            "#J_sel-invoice-pop-up-close click": "selInvoiceClose",//取消选择发票类型
            ".J_sel-invoice-item click": "selInvoiceItem",//选择发票类型
            "#J_sel-invoice-sure click": "selInvoiceSure",//确定选择
            "#J_submit-order click": "submitOrder",//提交订单
            ".J_normal-invoice-cate-sel click": "normalInvoiceCateSel",//普通发票个人单位选择

        },
        /**
         * 商品数量加减
         */
        goodsNumChange: function (e) {
            var self = $(e.currentTarget),
                $numPractical = $('#J_num-practical'),
                $numPracticalVal = $numPractical.val();

            if (self.data('type') < 0) {
                if ($numPracticalVal <= 1) {
                    this.priceShow($numPracticalVal);
                    return;
                }
                $numPractical.val(--$numPracticalVal);
                this.priceShow($numPracticalVal);
            } else {
                var maxInventory = $('#J_inventory').data('inventory');
                if ($numPracticalVal >= maxInventory) {
                    Tools.msg('所选商品不能大于库存数！');
                    $numPractical.val(maxInventory);
                    this.priceShow($numPracticalVal);
                    return;
                }
                $numPractical.val(++$numPracticalVal);
                this.priceShow($numPracticalVal);
            }

        },
        /**
         * 价格显示
         */
        priceShow(num){
            var $goodsPriceVal = $('#J_goods-price').text(),//单价
                bottomBuyInfo = $('#J_bottom-buy-info');
            bottomBuyInfo.find('.sum-to').find('.num').text(num);
            bottomBuyInfo.find('.sum-to').find('.price').text($goodsPriceVal);
            bottomBuyInfo.find('.total-price').text($goodsPriceVal * num);
        },
        /**
         * 选择收货地区
         */
        selectArea: function () {
            var selectArea = new MobileSelectArea();
            selectArea.init({
                trigger: '#J_get-area',
                value: $('#J_get-area-hide').val(),
                data: '../js/city_data.json',
                position: "bottom",
                callback: function (res) {
                    //保存选择地区
                    $('#J_get-area').keyup();
                }
            });
        },
        /**
         * 选择发票类型弹框 显示
         */
        selInvoiceClick: function () {
            $('#J_sel-invoice-pop-up').fadeIn();
        },
        /**
         * 取消选择发票类型
         */
        selInvoiceClose: function () {
            $('#J_sel-invoice-pop-up').fadeOut();
            $('#J_sel-invoice-txt').text('不开具发票');//发票类型
            $('#J_sel-invoice-final-sure').val('0');
            $('.J_no-invoice').addClass('cur').siblings().removeClass('cur');
            $('#J_invoice-type').find('.invoice-input').prop("disabled", true);
            var $invoiceType = $('#J_invoice-type'),
                $typeItem = $invoiceType.find('.type-item');
            $typeItem.hide();

        },
        /**
         * 选择发票类型
         * @param e
         */
        selInvoiceItem: function (e) {
            var self = $(e.currentTarget),
                $detailItem = self.parents('.detail-item'),
                $itemTxt = self.parents('.item-input-our').siblings('.item-txt').text(),
                $invoiceType = $('#J_invoice-type');
            $('#J_sel-invoice-final-sure').val(self.val());
            $('#J_sel-invoice-txt').text($itemTxt);//发票类型
            $detailItem.addClass('cur').siblings().removeClass('cur');

            //发票输入框不可用
            $('#J_invoice-type').find('.invoice-input').prop("disabled", true);
            if (self.val() == '1') {//普通发票
                $invoiceType.find('.normal-invoice').find('.invoice-input').prop("disabled", false);
            }
            if (self.val() == '2') {//增值税发票
                $invoiceType.find('.dedicated-invoice').find('.invoice-input').prop("disabled", false);
                console.log($invoiceType.find('.dedicated-invoice').find('.invoice-input').prop("disabled"))
            }

        },
        /**
         * 确定选择
         */
        selInvoiceSure: function (e) {
            // if (!$('#J_sel-invoice-final-sure').val()) {
            //     layer.open({
            //         content: '请选择是否开具发票！'
            //         , skin: 'msg'
            //         , time: 2 //2秒后自动关闭
            //     });
            //     return;
            // }
            var self = $(e.currentTarget),
                $invoiceType = $('#J_invoice-type'),
                $typeItem = $invoiceType.find('.type-item'),
                $selInvoiceFinalSure = $('#J_sel-invoice-final-sure');
            $typeItem.hide();
            if ($selInvoiceFinalSure.val() == '0') {//不开具发票

            }
            if ($selInvoiceFinalSure.val() == '1') {//普通发票
                $invoiceType.find('.normal-invoice').fadeIn();
            }
            if ($selInvoiceFinalSure.val() == '2') {//增值税发票
                $invoiceType.find('.dedicated-invoice').fadeIn();
            }
            $('#J_sel-invoice-pop-up').fadeOut();

        },
        /**
         * 提交订单验证
         */
        submitOrderCheck: function () {
//            var $getAreaHide = $('#J_get-area-hide').val();
//            if (!$getAreaHide) {
//                Tools.msg('请选择省份 城市 区县！')
//                return false;
//            }
//            var $detailAddress = $('#J_detail-address').val();
//            if (!$detailAddress) {
//                Tools.msg('请填写详细地址！')
//                return false;
//            }
//            var $recipients = $('#J_recipients').val();
//            if (!$recipients) {
//                Tools.msg('请填写收件人！')
//                return false;
//            }
//            var $tel = $('#J_tel').val();
//            if (!$tel) {
//                Tools.msg('请填写电话号码！')
//                return false;
//            }
//            var telRe = /^(86)?1[3,4,5,7,8]\d{9}$/;
//            if (!telRe.test($tel)) {
//                Tools.msg('请正确电话号码！')
//                return false;
//            }
            var selInvoiceFinalSure = $('#J_sel-invoice-final-sure'),
                isNeedInvoice = selInvoiceFinalSure.val() != '0';
            
            if ((isNeedInvoice && !$('.J_unit-name').val() && !$('.J_unit-name').prop('disabled')) || (isNeedInvoice && !$('.J_unit-name-normal').val() && !$('.J_unit-name-normal').prop('disabled')&&!$('.normal-invoice-unit-name').is(':hidden'))) {
                Tools.msg('请输入单位名称！')
                return false;
            }

            if (isNeedInvoice && !$('.J_normal-invoice-distinguish-name').val() && !$('.J_normal-invoice-distinguish-name').prop('disabled') &&!$('.J_normal-invoice-dedicated-invoice-info').is(':hidden')) {
                Tools.msg('请输入纳税人识别号！')
                return false;
            }

            if (isNeedInvoice && !$('.J_normal-invoice-cont').val() && !$('.J_normal-invoice-cont').prop('disabled')) {
                Tools.msg('请填写发票内容！')
                return false;
            }
            if (isNeedInvoice && !$('.J_dedicated-invoice-cont').val() && !$('.J_dedicated-invoice-cont').prop('disabled')) {
                Tools.msg('请填写发票内容！')
                return false;
            }
            if (isNeedInvoice && !$('.J_distinguish-name').val() && !$('.J_distinguish-name').prop('disabled')) {
                Tools.msg('请填写纳税人识别号！')
                return false;
            }

            var numRe = /^\d*$/;
            if (!numRe.test($('.J_distinguish-name').val())&& !$('.J_distinguish-name').prop('disabled')) {
                Tools.msg('请填写正确纳税人识别号！')
                return false;
            }

            if (isNeedInvoice && !$('.J_registered-address').val() && !$('.J_registered-address').prop('disabled')) {
                Tools.msg('请填写注册地址！')
                return false;
            }
            if (isNeedInvoice && !$('.J_registered-tel').val() && !$('.J_registered-tel').prop('disabled')) {
                Tools.msg('请填写注册电话！')
                return false;
            }
            var telRe02 = /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/
            if (isNeedInvoice&&!telRe02.test($('.J_registered-tel').val())&&!$('.J_registered-tel').prop('disabled')) {
                Tools.msg('请填写正确注册电话！')
                return false;
            }
            if (isNeedInvoice && !$('.J_deposit-bank').val() && !$('.J_deposit-bank').prop('disabled')) {
                Tools.msg('请填写开户银行！')
                return false;
            }
            if (isNeedInvoice && !$('.J_bank-account').val() && !$('.J_bank-account').prop('disabled')) {
                Tools.msg('请填写银行账号！')
                return false;
            }
            if (!numRe.test($('.J_bank-account').val())&& !$('.J_bank-account').prop('disabled')) {
                Tools.msg('请填写正确的银行账号！')
                return false;
            }
            return true;
        },
        /**
         * 提交订单
         */
        submitOrder: function () {
            if (!this.submitOrderCheck()) {
                return;
            }

            $.ajax({
                url: '',
                data: $('#J_order-form').serialize(),
                // dataType:'JSON'
                success: function (res) {
                    console.log(res, 'success')
                },
                error: function (res) {
                    console.log(res)

                }
            })
        },
        /**
         *普通发票个人单位选择
         */
        normalInvoiceCateSel: function (e) {
            var self = $(e.currentTarget),
                $normalInvoiceCateSel = $('.J_normal-invoice-cate-sel');
            $normalInvoiceCateSel.removeClass('cur');
            $normalInvoiceCateSel.find('.invoice-input').prop('checked', false);
            self.addClass('cur').find('.invoice-input').prop('checked', true);
            var $normalInvoiceUnitName = $('.normal-invoice-unit-name'),//增值税普通发票单位名称
                $normalInvoiceUnitNameInput = $normalInvoiceUnitName.find('input'),
                $normalInvoiceDedicatedInvoiceInfo=$('.J_normal-invoice-dedicated-invoice-info'),//增值税普通发票纳税人识别号
                $normalInvoiceDedicatedInvoiceInfoInput=$normalInvoiceDedicatedInvoiceInfo.find('input');
            if (self.data('invoicecate') == 1) {//个人
                $normalInvoiceUnitName.hide();
                $normalInvoiceUnitNameInput.prop("disabled", true);

                $normalInvoiceDedicatedInvoiceInfo.hide();
                $normalInvoiceDedicatedInvoiceInfoInput.prop("disabled", true)
            } else {//单位
                $normalInvoiceUnitName.show();
                $normalInvoiceUnitNameInput.prop("disabled", false);

                $normalInvoiceDedicatedInvoiceInfo.show();
                $normalInvoiceDedicatedInvoiceInfoInput.prop("disabled", false)
            }
        }

    });
    submitOrder.init();
}();